package console

import (
	"os"
	"os/exec"
	"runtime"
	"strings"
)

func Menu() {
	var menu = "功能\t\t\t命令\n" +
		"增加服务器\t\tadd OR add -ip [*ip] -a [alias] -n [name] -u [user] -p [password] -r [remark] \n" +
		"修改服务器信息\t\tedit OR edit -ip [*ip] -a [alias] -n [name] -u [user] -p [password] -r [remark] \n" +
		"列出服务器\t\tlist OR list -ip [ip] -a [alias] -n [name] -u [user] -p [password] -r [remark]\n" +
		"删除服务器\t\tdel OR del del [ip]\n" +
		"修改配置文件\t\tconf\n" +
		"退出\t\t\tq OR exit"
	logger.Println(menu)
}

func Clear() {
	switch runtime.GOOS {
	case "windows":
		cmd := exec.Command("cmd", "/c", "cls") //Windows example, its tested
		cmd.Stdout = os.Stdout
		var err = cmd.Run()
		if err != nil {
			logger.Println(err)
		}
	default:
		cmd := exec.Command("clear") //Linux example, its tested
		cmd.Stdout = os.Stdout
		var err = cmd.Run()
		if err != nil {
			logger.Println(err)
		}
	}
}

func Main() {
MAIN:
	var input = strings.Trim(Input("> "), " ")
	var div = strings.Index(input, " ")
	var cmd string
	var param string
	if div > 0 {
		cmd = input[0:div]
		param = input[div:]
	} else {
		cmd = input
		param = ""
	}
	cmd = strings.ToUpper(strings.Trim(cmd, " "))
	param = strings.Trim(param, " ")
	switch cmd {
	case "ADD":
		addServer(param)
		break
	case "EDIT":
		addServer(param)
		break
	case "DEL":
		delServer(param)
		break
	case "LIST":
		showServer(param)
		break
	case "Q":
		return
	case "EXIT":
		return
	case "MENU":
		Menu()
	case "HELP":
		Menu()
	case "CLEAR":
		Clear()
	default:
		logger.Println("\r未知的命令 >> " + input)
	}
	goto MAIN
}

func addServer(params string) bool {
	logger.Println("====== addServer ======")
	return false
}
func delServer(params string) bool {
	logger.Println("====== delServer ======")
	return false
}
func showServer(params string) bool {
	logger.Println("====== showServer ======")
	return false
}
